package com.talking.presenter;
import android.util.Log;

import com.talking.model.Http;
import com.talking.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;

import okhttp3.Callback;
public class UserPresenter  {
    public static void login(String account, String password, Callback callback) {
        Http.get("user/verify/?account="+account+"&password="+password, callback);
    }
    public static void sign(String account, String email, String password, String password2, Callback callback) {
        if (!account.equals("")&&!password.equals("")&&!email.equals("")){
            if (password.equals(password2)){
                if (!Utils.isEmail(email)){
                    callback.onFailure(null,new IOException("邮箱格式有误"));
                    return;
                }
                try {
                    JSONObject ja=new JSONObject();
                    ja.put("email",email);
                    ja.put("account",account);
                    ja.put("password",password);
                    Http.post(ja, "user/sign/",callback);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }else {
                callback.onFailure(null,new IOException("两次密码不一样"));
            }
        }else {
            callback.onFailure(null,new IOException("请输入完整"));
        }
    }
    public static void changePassword(String email,String password,Callback callback){
        Http.get( "user/changePasswordByEmail/?mail="+email+"&password="+password, callback);
    }
    public static void getUserByAccount(String account,String whoseFriend,Callback callback){
        if (whoseFriend==null){
            Http.get("user/getUserMsgByAccount/?account="+account, callback);
        }else{
            Http.get("user/getUserMsgByAccount/?account="+account+"&whoseFriend="+whoseFriend, callback);
        }
    }
    public static void modifyByEmail(String email,String password,String nickname,Callback callback){
        Http.get("user/modifyByEmail/?email="+email+"&password="+password+"&nickname="+nickname,callback);
    }
    public static void uploadIcon(String url, File file, Callback callback){
        Http.upload(url,file,callback);
    }
    public static void searchUser(int start, int end, String word, Callback callback) {
        Http.get("user/searchUsers/?start="+start+"&end="+end+"&word="+word,callback);
    }
    public static void sendEmail(String mail, String yzm, Callback callback) {
        Http.get("user/sendEmail/?mail="+mail+"&yzm="+yzm,callback);
    }
}